import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../layout/home.vue'
import Page from '../views/Homepage/index.vue'
import local from '../utils/local.js'

// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        component: Home,
        redirect: '/homepage',
        children: [{
                path: '/homepage',
                component: Page
            },
            {
                path: '/order',
                meta: { name: '订单管理' },
                component: () =>
                    import ( /* webpackChunkName: "about" */ '../views/Order/index.vue')
            },
            {
                path: '/Goods',
                meta: { name: '商品管理' },
                redirect: '/Goods',
                component: () =>
                    import ( /* webpackChunkName: "about" */ '../views/Goods/page.vue'),
                children: [{
                        path: '/Goods/index',
                        meta: { name: '商品列表' },
                        component: () =>
                            import ( /* webpackChunkName: "about" */ '../views/Goods/index.vue')
                    },
                    {
                        path: '/Goods/index2',
                        meta: { name: '商品添加' },
                        component: () =>
                            import (
                                /* webpackChunkName: "about" */
                                '../views/Goods/index2.vue'
                            )
                    },
                    {
                        path: '/Goods/index3',
                        meta: { name: '商品分类' },
                        component: () =>
                            import (
                                /* webpackChunkName: "about" */
                                '../views/Goods/index3.vue'
                            )
                    }
                ]
            },
            // 店铺管理
            {
                path: '/Store',
                meta: { name: '店铺管理' },
                component: () =>
                    import ( /* webpackChunkName: "about" */ '../views/Store/index.vue')
            },
            // 账户管理
            {
                path: '/Account',
                meta: { name: '账户' },
                redirect: '/Account',
                component: () =>
                    import ( /* webpackChunkName: "about" */ '../views/Account/page.vue'),
                children: [{
                        path: '/Account/index',
                        meta: { name: '账号列表' },
                        component: () =>
                            import (
                                /* webpackChunkName: "about" */
                                '../views/Account/index.vue'
                            )
                    },
                    {
                        path: '/Account/index2',
                        meta: { name: '添加账号' },
                        component: () =>
                            import (
                                /* webpackChunkName: "about" */
                                '../views/Account/index2.vue'
                            )
                    },
                    {
                        path: '/Account/personal',
                        meta: { name: '个人中心' },
                        component: () =>
                            import (
                                /* webpackChunkName: "about" */
                                '../views/Account/personal.vue'
                            )
                    },
                    {
                        path: '/Account/index3',
                        meta: { name: '修改密码' },
                        component: () =>
                            import (
                                /* webpackChunkName: "about" */
                                '../views/Account/index3.vue'
                            )
                    }
                ]
            },
            // 销售统计
            {
                path: '/Sales',
                meta: { name: '销售统计' },
                redirect: '/Sales',
                component: () =>
                    import ( /* webpackChunkName: "about" */ '../views/Sales/page.vue'),
                children: [{
                        path: '/Sales/index',
                        meta: { name: '销售1' },
                        component: () =>
                            import ( /* webpackChunkName: "about" */ '../views/Sales/index.vue')
                    },
                    {
                        path: '/Sales/index2',
                        meta: { name: '销售2' },
                        component: () =>
                            import (
                                /* webpackChunkName: "about" */
                                '../views/Sales/index2.vue'
                            )
                    }
                ]
            }
        ]
    },
    {
        path: '/login',
        component: () =>
            import ( /* webpackChunkName: "login" */ '../views/Login.vue')
    }
]

// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')

const router = new VueRouter({
        routes
    })
    //导航守卫 目的为不允许未登录的用户进入路由

router.beforeEach((to, from, next) => {
    const token = local.get('token')
    if (token) {
        next()
    } else {
        if (to.path === '/login') {
            next()
        } else {
            next('/login')
        }
    }
})

export default router